#include "Material.h"
#include "Illumination.h"
#include "Light.h"
#include "Scene.h"

const char *values[4] = { "r", "g", "b", "a" };
map<string, void (*)(TiXmlElement*, float*)> tag_parser;

void parseMaterialRGBA(TiXmlElement *elem, float *atr) {
	for(int i = 0; i < 4; ++i)
		elem->QueryFloatAttribute(values[i], &atr[i]);
}

void parseMaterialShininess(TiXmlElement *elem, float *atr) {
	elem->QueryFloatAttribute("value", atr);
}

void initMaterialAttributes() {
	if( tag_parser.empty() ) {
		tag_parser["emission"] = parseMaterialRGBA;
		tag_parser["ambient"] = parseMaterialRGBA;
		tag_parser["diffuse"] = parseMaterialRGBA;
		tag_parser["specular"] = parseMaterialRGBA;
		tag_parser["shininess"] = parseMaterialShininess;
	}
}

Material::Material(TiXmlElement *element) {
	map<string, float*> attributes;
	
	attributes["emission"] = this->emission;
	attributes["ambient"] = this->ambient;
	attributes["diffuse"] = this->diffuse;
	attributes["specular"] = this->specular;
	attributes["shininess"] = &this->shininess;
	
	initMaterialAttributes();
	id = Scene::testStringAttribute(element, "id");
	
	for(TiXmlElement *child = element->FirstChildElement(); child != NULL; child = child->NextSiblingElement())
		tag_parser[child->Value()](child, attributes[child->Value()]);

}

void Material::printMaterial() {
		
	cout << "ID: " << id << " shininess: " << shininess << endl;
	//Light::printComponents(lightComps);
	//Light::printRGBA(emission);
}